<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>添加类别</title>
    <link rel="stylesheet" href="../static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../static/css/public.css" media="all">
    <script src="/static/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
</head>
<style>
    body{
        background-color: white;
    }
</style>
<body>
<div class="layui-form">
    <form id="addType" class="layui-form" action="" method="get">
        <input type="hidden" id="typeId" name="typeId"/>
        <div class="layui-form-item" style="margin-top: 50px">
            <label class="layui-form-label">类别名称：</label>
            <div class="layui-input-block">
                <input id="typeName" name="typeName"  lay-verify="required" type="text" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text" style="margin-top: 30px">
            <label class="layui-form-label">类别描述：</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" id="description" name="description"  lay-verify="required" cols="62" rows="6"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="margin: auto;text-align: center;">
                <button id="subbtn" class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn"></button>
            </div>
        </div>
    </form>
</div>
<script src="/static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script th:inline="javascript" type="text/javascript">
    let data={
        typeId: null,
        typeName: null,
        description: null
    }
    $(function (){
        let courseType=[[${courseType}]];
        console.log(courseType);
        let submitUrl;
        if([[${courseType}]]===null){
            $('#subbtn').html("确认添加");
            submitUrl="/type/addType";
        }else{
            $('#typeId').val(courseType.typeId);
            $('#typeName').val(courseType.typeName);
            $('#description').val(courseType.description);
            $('#subbtn').html("确认修改");
            submitUrl="/type/editType";
        }

        $('#addType').submit(function (e){
            let formData=new FormData(this);
            data.typeId=formData.get("typeId");
            data.typeName=formData.get("typeName");
            data.description=formData.get("description");

            console.log("submitUrl="+submitUrl);
            $.ajax({
                type:"post",
                url:submitUrl,
                data:data,
                success:function (res){
                    if(res.code===200){
                        layer.msg(res.message,{time:5000,icon:1,offset:[15]});
                        let index=parent.layer.getFrameIndex(window.name);
                        setTimeout(function (){
                            window.parent.location.reload();//刷新父页面
                            parent.layer.close(index);
                        },1500);
                    }else{
                        layer.msg(res.message,{time:5000,icon:1,offset:[15]});
                    }
                },
                error:function (error){
                    layer.msg(error,{time:5000,icon:2,offset:[15]})
                }
            })
            e.preventDefault();
        })
    })
</script>
</body>
</html>